// 导入模块
import database from "../../utils/database";
// 数据库中运营位表的表名
const categoryTableName = 'Category';
// 数据库中商品表的表名
const goodsTableName = 'Product';
// 定义商品类别模型接口
export interface CategoryItem {
    id?: number;     // 标识
    name: string;    // 名称
    description: string; // 描述
    created_at?: string; // 创建时间
    sort: number;    // 排序权重
}
// 定义商品模型接口
export interface GoodsItem {
    id?: number;     // 标识
    name: string;    // 名称
    description: string; // 描述
    created_at?: string; // 创建时间
    detail: string;      // 商品详情
    category: CategoryItem// 商品类别
    price: number;       // 商品价格
    discounted_price: number; // 商品打折价格
    stock: number;       // 商品库存
    image: string;       // 商品图片
    status: number;      // 商品状态
    brand: string;       // 商品品牌
}
// 商品服务类
export class GoodsService {
    // 获取所有类别数据
    static getAllCategory() {
        return new Promise((resolve, reject) => {
            database.queryData(categoryTableName, ['*'], 'id is not null order by sort desc', (data) => {
                if (!data) {
                    reject('获取类别数据失败');
                } else {
                    const result = data.result;
                    resolve(result);
                }
            });
        });
    }
    // 新增类别
    static addCategory(ca: CategoryItem) {
        return new Promise((resolve, reject) => {
            const keys = ['name', 'description', 'sort'];
            const values = [ca.name, ca.description, ca.sort];
            database.insertData(categoryTableName, keys, values, (data) => {
                if (!data) {
                    reject('创建类别失败');
                } else {
                    resolve(null);
                }
            });
        });
    }
    // 更新类别
    static updateCategory(ca: CategoryItem) {
        return new Promise((resolve, _reject) => {
            database.updateData(categoryTableName, `name='${ca.name}', description='${ca.description}', sort=${ca.sort}`, `id=${ca.id}`);
            resolve(null);
        });
    }
    // 新增商品
    static addGoods(g: GoodsItem) {
        return new Promise((resolve, reject) => {
            const keys = ['name', 'description', 'detail', 'category_id', 'price', 'discounted_price', 'stock', 'image', 'status', 'brand'];
            const values = [g.name, g.description, g.detail, g.category.id, g.price, g.discounted_price, g.stock, g.image, g.status, g.brand];
            database.insertData(goodsTableName, keys, values, (data) => {
                if (!data) {
                    reject('创建商品失败');
                } else {
                    resolve(null);
                }
            });
        });
    }
    // 更新商品
    static updateGoods(g: GoodsItem) {
        return new Promise((resolve, _reject) => {
            database.updateData(goodsTableName, `name='${g.name}', description='${g.description}', detail='${g.detail}', category_id=${g.category.id}, stock=${g.stock}, image='${g.image}', status=${g.status}, brand='${g.brand}', price=${g.price}, discounted_price=${g.discounted_price}`, `id = ${g.id}`);
            resolve(null);
        });
    }
    // 删除商品
    static deleteGoods(id: number) {
        return new Promise((resolve, _reject) => {
            database.deleteData(goodsTableName, `id=${id}`);
            resolve(null);
        });
    }
    // 获取商品详情
    static getGoodsDetail(id: number) {
        return new Promise((resolve, reject) => {
            database.queryDataFrom(goodsTableName, ['*'], [['id', 'name', 'description', 'sort', 'created_at']], [categoryTableName], ['category_id'], ['category'], `id=${id}`, (data) => {
                if (!data) {
                    reject('获取商品失败');
                } else {
                    const result = data.result;
                    resolve(result);
                }
            });
        });
    }
    // 获取商品列表
    static getGoodList(cid: number | undefined = undefined, offset: number, limit: number) {
        return new Promise((resolve, reject) => {
            const keys = ['id', 'created_at', 'name', 'description', 'category_id', 'price', 'discounted_price', 'stock', 'image', 'status', 'brand'];
            let where = '';
            if (cid) {
                where += `category_id=${cid}`;
            } else {
                where += 'id IS NOT NULL';
            }

            where += ` limit ${limit} offset ${offset}`;
            database.queryDataFrom(goodsTableName, keys, [['id', 'name', 'description', 'sort', 'created_at']], [categoryTableName], ['category_id'], ['category'], where, (data) => {
                if (!data) {
                    reject('获取商品数据失败');
                } else {
                    const result = data.result;
                    resolve(result);
                }
            });
        });
    }
}